cc.Class({
    extends: cc.Component,

    properties: {
        //网的预制件
        flueFabs:[cc.Prefab],
        coinFab:cc.Prefab,
        scoreFab:cc.Prefab,
        //最大鱼的预制件
        fishFab:cc.Prefab,
       
    },

   
    onLoad: function () {  
        //生成最大鱼的预制件
        this.maxFish=cc.instantiate(this.fishFab);

    },
    fishAction:function(type,isLeft){

        this.leftNum=isLeft;
        //鱼的种类
        this.typeNum=type;

        //定义每条生成的鱼的角度
        this.rotation = window.Global.getRandomRotation(-45,45);
       
        // cc.log(this.rotation);
        var randonSpeed = window.Global.getRandomSpeed(window.Global.fishData[type].maxSpeed,window.Global.fishData[type].maxSpeed);
        if(isLeft===-1)
        {
            this.node.rotation=this.rotation;
            this.speedX = randonSpeed*Math.cos(this.rotation/180*Math.PI); 
        }
        else
        {
            this.node.rotation=180-this.rotation;
            this.speedX = -randonSpeed*Math.cos(this.rotation/180*Math.PI);
        }
        this.speedY = -randonSpeed*Math.sin(this.rotation/180*Math.PI);

        //随时转换鱼的角度
        this.schedule(this.channgeDirector.bind(this),1);

    },

    channgeDirector:function(){
        var tempRotation = this.node.rotation;

        var changeRotation = tempRotation + window.Global.getRandomRotation(-45,45);
        // cc.log(changeRotation);
        //每隔一秒改变转向
        // this.node.runAction(cc.rotateBy(1.5,changeRotation));
        //每隔一秒改变移动的速度方向

        // var randonSpeed = window.Global.getRandomSpeed(window.Global.fishData[this.typeNum].maxSpeed,window.Global.fishData[this.typeNum].maxSpeed);
        // if(this.leftNum===-1)
        // {
        //     this.node.rotation = changeRotation;
        //     this.speedX = randonSpeed*Math.cos(changeRotation/180*Math.PI);
        // }
        // else
        // {
        //     this.node.rotation= 180-changeRotation;
        //     this.speedX = -randonSpeed*Math.cos(changeRotation/180*Math.PI);
        // }
       
        // this.speedY = -randonSpeed*Math.sin(this.rotation/180*Math.PI);



    },
   
    update: function (dt) { 
        this.node.x+=this.speedX;
        this.node.y+=this.speedY;

        
        //鱼游出屏幕自动删除
        if(this.node.x <= -(cc.winSize.width/2+50+this.maxFish.width) || 
           this.node.x >= cc.winSize.width/2+50+this.maxFish.width || 
           this.node.y >= (610+this.maxFish.height) ||this.node.y <= -(610+this.maxFish.height))
        {
            this.node.destroy();
        }
    },

    //碰撞函数:在鱼的函数里写的碰撞，self:是鱼，other是炮弹
    onCollisionEnter:function(other,self)
    {
        //cc.log("碰撞开始");
        //炮弹删除
        other.node.destroy();

        //判断鱼是否被捕获
        //捕获公式 鱼的捕获率 * （1 + 炮弹的编号 * 0.05） > Math.random() ; //true 捕获成功 false 捕获失败
        var capture = window.Global.fishData[this.typeNum].captureRate* (1 + cc.find("Canvas").getComponent("Game").batteryCurrentIndex * 0.05) > Math.random();

        // var capture=true;
        this.fishAminType=this.typeNum+1;

        //生成网
        var flueType= cc.find("Canvas").getComponent("Game").batteryCurrentIndex;
        //渔网生成随动画删除
        //添加预制件网实例化后的节点到舞台并返回该节点及父类对象
        var flue= window.Global.getClassAndAdd(this.flueFabs[flueType],"flue",other.node.parent,other.node.x,other.node.y);
        flue.tNode.zIndex = self.node.zIndex+1;

        //捕获成功
        if(capture)
        {
           
            //停止update；让鱼不动
            this.enabled=false;
            //播放当前鱼的死亡动画
            
            this.getComponent(cc.Animation).play("fish"+this.fishAminType+"_1");

            //记录鱼类当前死亡位置,将鱼的坐标转换为bg/bar上的坐标，把金币预制件实例化到bg/bar的鱼的坐标上，然后将金币飘到bg/bar/gold的位置
            // this.fishDiedPos = cc.p(this.node.x,this.node.y);

            //将鱼的坐标转换为bg/bar上的坐标
            //cc.log(this.node.position);
            var fishDiedPosChanged = cc.find("Canvas/bg/bar").convertToNodeSpaceAR(this.node.convertToWorldSpaceAR());
            //cc.log(fishDiedPosChanged);

            //金币动画：
            //把金币预制件实例化到bg/bar的鱼的坐标上
            var glodCoin= window.Global.getClassAndAdd(this.coinFab,"CionMove",cc.find("Canvas/bg/bar"),fishDiedPosChanged.x,fishDiedPosChanged.y);

            var action1=cc.moveTo(1,cc.find("Canvas/bg/bar/gold").position);
            var callBack1=cc.callFunc(function(){
                glodCoin.tNode.destroy();
            }.bind(this))
            //金币生成就移动到gold节点坐标的位置
            glodCoin.tNode.runAction(cc.sequence(action1,cc.delayTime(0.1),callBack1));


            //分数动画：
             //把分数预制件实例化到bg/bar的鱼的坐标上
             var scoreCoin= window.Global.getClassAndAdd(this.scoreFab,"flue",cc.find("Canvas/bg/bar"),fishDiedPosChanged.x,fishDiedPosChanged.y);
            //设置不同鱼的分数值
             var score = window.Global.fishData[this.typeNum].coin;
             var fishScore = scoreCoin.tNode.getComponent(cc.Label).string= "+" + score;

             //将分数所得分数加载到金币槽上
             cc.find("Canvas").getComponent("Game").gold += Number(fishScore);
            

           
        }
    },
    onCollisionStay:function(other,self)
    {
        //cc.log("碰撞保持");

    },
    onCollisionExit:function(other,self)
    {
        //cc.log("碰撞结束");

    },
});
